package com.topcheer.common.base.exception;

import com.topcheer.common.base.vo.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * @author xutao
 * @Package com.topcheer.common.base.exception
 * @Description: ${todo}
 * @date 2020/3/30 10:22
 */
@ControllerAdvice
@Slf4j
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public BaseResponse businessExceptionHandler(HttpServletResponse response, BusinessException ex) {
        //        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        log.error(ex.getMessage(), ex);
        return new BaseResponse().code(ex.getCode()).msg(ex.getMessage());
    }

    @ExceptionHandler(UserAuthException.class)
    public BaseResponse clientTokenExceptionHandler(HttpServletResponse response, UserAuthException ex) {
        //        response.setStatus(ex.getCode());
        log.error(ex.getMessage(), ex);
        return new BaseResponse().code(ex.getCode()).msg(ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public BaseResponse exceptionHandler(HttpServletResponse response, Exception ex) {
        //        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        log.error(ex.getMessage(), ex);
        return new BaseResponse().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).msg(ex.getMessage());
    }
}
